﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Data.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace UI.Forms
{
    public partial class printwhd_fk :FormBase1
    {
        BindingList<SVC.client.View_whd_d_1> vd1s;
        BindingList<Report.外乎分配单> whfks;
        public printwhd_fk()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DateTime? sd = null;
            DateTime? ed = null;
            Guid? infoempid = null;
            if (dateselect1.de_sd.EditValue != null && dateselect1.de_ed.EditValue != null)
            {
                sd = dateselect1.de_sd.DateTime;
                ed = dateselect1.de_ed.DateTime;
            }
            vd1s.Clear();
            foreach (SVC.client.View_whd_d_1 item in my.bw.View_whd_d_1_get_pid(te_pid.Text, sd, ed,1,null,  my.ACID))
	        {
                vd1s.Add(item);
	        }

            
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button==MouseButtons.Left&&e.Clicks==2)
            {
                if (e.RowHandle>=0)
                {
                    if (!dxValidationProvider1.Validate()) return;
                    SVC.client.View_whd_d_1 vd = gridView1.GetRow(e.RowHandle) as SVC.client.View_whd_d_1;
                    if (vd == null) return;
                    if (whfks.Count(p => p.编号 == vd.did) > 0) return;
                    Report.外乎分配单 newitem = new Report.外乎分配单();
                    newitem.编号 = vd.did;
                    newitem.打印日期 = DateTime.Now;
                    newitem.带宽 = vd.带宽;
                    newitem.地址 = vd.地址;
                    newitem.交流信息 = vd.交流信息;
                    newitem.业务员 = lookUpEdit1.Text;
                    newitem.业务员ID = new Guid(lookUpEdit1.EditValue.ToString());
                    whfks.Add(newitem);
                }
            }
        }

        private void printwhd_fk_Load(object sender, EventArgs e)
        {
            lookUpEdit1.Properties.DataSource = my.bw.View_Emp_lite_List("", my.ACID);
            vd1s = new BindingList<SVC.client.View_whd_d_1>();
            whfks = new BindingList<Report.外乎分配单>();
            print1.BillTypeID = 5;
            print1.FatherPrint = Print;
            print1.FatherDesign = Design;
            print1.Bind();
            gridControl1.DataSource = vd1s;
            gridControl2.DataSource = whfks;
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="xr1">报表</param>
        /// <param name="type">0=打印预览 1=打印对话框</param>
        private void Print(DevExpress.XtraReports.UI.XtraReport xr1, int type)
        {
            UI.Report.Templet.外乎分配单 report = (UI.Report.Templet.外乎分配单)xr1;
            report.bindingSource1.DataSource = whfks;
            report.FillDataSource();
            
            switch (type)
            {
                case 0:
                    report.ShowPreviewDialog();
                    break;
                case 1:
                    report.PrintDialog();
                    rewrite();
                    break;
                case 2:
                    if (!string.IsNullOrEmpty(my.myprint))
                    {
                        report.Print(my.myprint);
                        rewrite();
                    }
                    else
                    {
                        Comm.ShowAlert("请先设置本系统默认打印机!");
                    }
                    break;
                default:
                    break;
            }
        }

        private void Design(DevExpress.XtraReports.UI.XtraReport xr, SVC.client.RP_Temple rr, int editMode)
        {
            UI.Report.Templet.外乎分配单 report = (UI.Report.Templet.外乎分配单)xr;
            report.bindingSource1.DataSource = whfks;
            report.FillDataSource();

            ReportDesigner rd = null;
            switch (editMode)
            {
                case 0:
                    rd = new ReportDesigner(report, rr, true);
                    rd.FatherRefresh = print1.Bind;
                    rd.ShowDialog(this);
                    break;
                case 1:
                    rd = new ReportDesigner(report, rr, false);
                    rd.FatherRefresh = print1.Bind;
                    rd.ShowDialog(this);
                    break;
                default:
                    break;



            }
        }

        private void rewrite()
        {
            BindingList<int> dids = new BindingList<int>();
            BindingList<Guid> empids = new BindingList<Guid>();
            foreach (var item in whfks)
            {
                dids.Add(item.编号);
                empids.Add(item.业务员ID);
            }
            string r = my.bw.afterprintwirtefk(dids, empids, my.ACID);
            if (string.IsNullOrEmpty(r))
            {
                simpleButton1_Click(null,null);
                whfks.Clear();
            }
            else
            {
                Comm.ShowErorr(r);
            }
        }
    }
}